<?php
/**
 * Created by PhpStorm.
 * User: zyh
 * Date: 2018/5/3
 * Time: 下午6:54
 */

namespace app\index\command;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
use Workerman\Worker;

class workman extends Command
{

    public function __construct($name = null)
    {
        parent::__construct($name);


        if(strpos(strtolower(PHP_OS), 'win') === 0)
        {
            exit("Only Run at linux/unix \n");
        }

        // 检查扩展
        if(!extension_loaded('pcntl'))
        {
            exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        }

        if(!extension_loaded('posix'))
        {
            exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        }
    }

    protected function configure()
    {
        $this->setName("workerman");
        $this->setDescription("斗蟋蟀的SOCKET网关服务");
        $this->addArgument("status",Argument::OPTIONAL,"status");
    }

    protected function execute(Input $input, Output $output)
    {
        //注册服务
        $register = new Register('text://0.0.0.0:1236');
//        $register->listen();
        //网关服务

        $context = array(
            // 更多ssl选项请参考手册 http://php.net/manual/zh/context.ssl.php
            'ssl' => array(
                // 请使用绝对路径
                'local_cert'                 => '/www/server/panel/vhost/ssl/wss.oaaao.cn/fullchain.pem', // 也可以是crt文件
                'local_pk'                   => '/www/server/panel/vhost/ssl/wss.oaaao.cn/privkey.pem',
                'verify_peer'                => false,
                // 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
            )
        );

        $gateway = new Gateway("websocket://0.0.0.0:8282",$context);
        $gateway->transport = 'ssl';
        $gateway->name = "Gateway";
        $gateway->count = 8;
        $gateway->lanIp = "127.0.0.1";
        $gateway->startPort = 7500;
        $gateway->registerAddress = "127.0.0.1:1236";
        $gateway->pingInterval = 5;
        $gateway->pingData = base64_encode('{"type":"ping"}');
//        $gateway->pingNotResponseLimit = 1;

        //业务服务
        $worker = new BusinessWorker();
        $worker->name = "BusinessWorker";
        $worker->count = 4;
        $worker->registerAddress = '127.0.0.1:1236';
        $worker->eventHandler = 'app\index\command\Events';
        global $argv;
        array_shift($argv);
        Worker::runAll();
    }
}